package utils

import (
	"bufio"
	"encoding/json"
	"gitee.com/mjun1833/buildnotification/model"
	log "github.com/sirupsen/logrus"
	"io"
	"os"
	"strings"
)

func Split() {
	//BuildData()
	file, err := os.Open("C:\\Users\\junma\\Desktop\\nanjinRequest.log")
	if err != nil {
		panic(err)
	}
	defer file.Close()

	reader := bufio.NewReader(file) //建立缓冲区,将文件内容放入到缓冲区
	for {
		line, err := reader.ReadString('\n') //读到一个换行就结束
		if err != nil {
			if err == io.EOF { //io.EOF 表示文件的末尾
				if len(line) != 0 {
					err := SplitData(line)
					if err != nil {
						panic(err)
					}
				}
				log.Infof("文件读取完毕")
				break
			}
			panic(err)
		}
		err = SplitData(line)
		if err != nil {
			panic(err)
		}
	}
}

func SplitData(line string) error {
	line1 := strings.ReplaceAll(line, " ", "")
	line2 := strings.ReplaceAll(line1, "\r", "")
	line3 := strings.ReplaceAll(line2, "\n", "")
	line4 := strings.ReplaceAll(line3, "\\", "")

	if line4 == "" || line4 == "\"" {
		return nil
	}

	notifications := &model.CreateSubscribeNotificationArrReq{}
	err := json.Unmarshal([]byte(line4), notifications)
	if err != nil {
		log.Errorf("line: %v,unmarshal fail,err: %v", line, err)
		return nil
	}

	if len(notifications.SubscribeNotificationListObject.SubscribeNotificationObject) == 0 {
		return nil
	}

	for _, notification := range notifications.SubscribeNotificationListObject.SubscribeNotificationObject {
		if notification.FaceObjectList != nil && len(notification.FaceObjectList.FaceObject) != 0 {
			for _, face := range notification.FaceObjectList.FaceObject {
				marshal, err := json.Marshal(face)
				if err != nil {
					return err
				}
				err = BufWriter("C:\\Users\\junma\\Desktop\\buildnotification\\face.log", marshal)
				if err != nil {
					return err
				}
			}
		}
		if notification.PersonObjectList != nil && len(notification.PersonObjectList.PersonObject) != 0 {
			for _, person := range notification.PersonObjectList.PersonObject {
				marshal, err := json.Marshal(person)
				if err != nil {
					return err
				}
				err = BufWriter("C:\\Users\\junma\\Desktop\\buildnotification\\person.log", marshal)
				if err != nil {
					return err
				}
			}
		}
		if notification.MotorVehicleObjectList != nil && len(notification.MotorVehicleObjectList.MotorVehicleObject) != 0 {
			for _, vehicle := range notification.MotorVehicleObjectList.MotorVehicleObject {
				marshal, err := json.Marshal(vehicle)
				if err != nil {
					return err
				}
				err = BufWriter("C:\\Users\\junma\\Desktop\\buildnotification\\vehicle.log", marshal)
				if err != nil {
					return err
				}
			}
		}
		if notification.NonMotorVehicleObjectList != nil && len(notification.NonMotorVehicleObjectList.NonMotorVehicleObject) != 0 {
			for _, nonMotorVehicle := range notification.NonMotorVehicleObjectList.NonMotorVehicleObject {
				marshal, err := json.Marshal(nonMotorVehicle)
				if err != nil {
					return err
				}
				err = BufWriter("C:\\Users\\junma\\Desktop\\buildnotification\\nonMotorVehicle.log", marshal)
				if err != nil {
					return err
				}
			}
		}
	}
	return nil
}

func BufWriter(fileName string, line []byte) error {
	file, err := os.OpenFile(fileName, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
	if err != nil {
		return err
	}

	writer := bufio.NewWriter(file)
	_, err = writer.Write(line)
	if err != nil {
		return err
	}
	_, err = writer.WriteString("\n")
	if err != nil {
		return err
	}

	err = writer.Flush()
	if err != nil {
		return err
	}

	return nil
}

//func ReadFile(path string, dataType DataType, dataChan chan CaptureData) error {
//	file, err := os.Open(path)
//	if err != nil {
//		return err
//	}
//	defer file.Close()
//
//	reader := bufio.NewReader(file) //建立缓冲区,将文件内容放入到缓冲区
//	for {
//		line, err := reader.ReadBytes('\n') //读到一个换行就结束
//		if err != nil {
//			if err == io.EOF { //io.EOF 表示文件的末尾
//				if len(line) != 0 {
//					err = SendDataToChan(line, dataType, dataChan)
//					if err != nil {
//						return err
//					}
//				}
//				log.Infof("地址: %v 文件读取完毕", path)
//				break
//			}
//			return err
//		}
//
//		err = SendDataToChan(line, dataType, dataChan)
//		if err != nil {
//			return err
//		}
//	}
//
//	return nil
//}
